package cn.yc.oauth2.cloud.auth.server.controller;

import cn.yc.oauth2.cloud.auth.common.model.LoginVal;
import cn.yc.oauth2.cloud.auth.common.model.ResultMsg;
import cn.yc.oauth2.cloud.auth.common.model.SysConstant;
import cn.yc.oauth2.cloud.auth.common.utils.OauthUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author huweihua
 * @date 2022/10/21
 */
@RestController
@RequestMapping("/oauth")
@Slf4j
@RequiredArgsConstructor
public class AuthController {

    private final StringRedisTemplate stringRedisTemplate;


    @PostMapping("/logout")
    public ResultMsg logout() {
        LoginVal loginVal = OauthUtils.getCurrentUser();
        log.info("令牌唯一ID：{},过期时间：{}", loginVal.getJti(), loginVal.getExpireIn());
        //这个jti放入redis中，并且过期时间设置为token的过期时间
        stringRedisTemplate.opsForValue().set(SysConstant.JTI_KEY_PREFIX + loginVal.getJti(), "", loginVal.getExpireIn(), TimeUnit.SECONDS);
        return new ResultMsg(200, "注销成功", null);
    }
}
